import operate from '@/common/operate.js'
/**
 * 发送请求
 */
export default function request(api, method, data = {}) {
	// console.log(data, api);
	let header
	let HTTP_REQUEST_URL = operate.api
	//let token = uni.getStorageSync(TOKEN_NAME) || null
	let CONTENT_TYPE = 'application/json' // 'application/x-www-form-urlencoded' 
	header = {
		'Content-Type': CONTENT_TYPE
	}

	return new Promise((reslove, reject) => {
		uni.showNavigationBarLoading()
		console.log("request")
		uni.request({
			url: HTTP_REQUEST_URL + api,
			method: method || 'GET',
			header: header,
			data,
			success: (res) => {
				console.log('接口请求成功', res);
				if (!res.data) {
					reject('请求失败');
					uni.hideNavigationBarLoading()
				} else {
					uni.hideNavigationBarLoading()
					if (res.data.code === 0) { //成功
						reslove(res.data);
					} else {
						console.log('接口请求失败', res);
						uni.hideNavigationBarLoading()
						reject(res.data);
						if (getApp().globalData.userInfo === '') {
							uni.reLaunch({
								url: '/pages/loginOnlyPhone/loginOnlyPhone'
							})
						}
					}
				}
			},
			fail: (msg) => {
				console.log('接口请求失败', msg);
				uni.hideNavigationBarLoading()
				reject('请求失败');
			}
		})
	});
}

['options', 'get', 'post', 'put', 'head', 'delete', 'trace', 'connect'].forEach((method) => {
	request[method] = (api, data) => request(api, method, data)
});
